<?php
// @formatter:off
/**
 * @file c.tag.php
 *
 * @author Alejandro Dario Simi
 * @date $Date: 2013-06-16 23:56:49 +0000 (Sun, 16 Jun 2013) $
 *
 * $Id: c.tag.php 73 2013-06-16 23:56:49Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/tags/wcomix-1.0.0.2/themes/mdefault/controlers/contents/c.tag.php $
 */
// @formatter:on

global $wcTag;
global $wcReadRate;

if($wcTag && $wcProfile->allowedTo(WC_PERM_TAG_VIEW)) {
	require_once "{$this->_themeRoot}/controlers/generics/pager.php";

	global $wcDefaults;
	global $wcPageCache;
	global $wcTagsHolder;
	global $wctDefaults;

	$wcPageCache->anyTag();

	$page = isset($_REQUEST["page"]) ? $_REQUEST["page"] : 1;
	$minTagInPage = $wctDefaults["max-per-page"]["tag"] * ($page - 1);
	$maxTagInPage = $minTagInPage + $wctDefaults["max-per-page"]["tag"];

	$wcThemeAssigns["PATH"] = "{$wcTag->name}";
	$wcThemeAssigns["ACTIONCONTENT"] = "/generics/list-view.html";

	//
	// Selecting only tags in range.
	$comics = array();
	$comicCount = 0;
	$items = $wcTag->membersFull();
	uasort($items, "WCComic::NameSort");
	foreach($items as $comic) {
		if($comicCount >= $minTagInPage && $comicCount < $maxTagInPage) {
			if($comic->ok()) {
				$comics[] = $comic;
			} else {
				$comicCount--;
			}
		}
		$comicCount++;
	}

	$pages = ceil($comicCount / $wctDefaults["max-per-page"]["tag"]);
	wcSetPager(requestParams(true, $wcAction, array('page')), $pages, $page);

	if($pages > 1) {
		$wcThemeAssigns["PAGER-SHOW"] = true;
		$wcThemeAssigns["PAGER-CURRENT"] = $page;
		$wcThemeAssigns["PAGER-COUNT"] = $pages;
	}

	//
	// Creating multi-array by prefix
	$lists = array();
	$ids = 1;
	$gid = $wcGroup->id();
	$iconId = 1;
	$zoom = array();
	foreach($comics as $comic) {
		$name = $comic->name;
		$prefix = strtoupper(isset($name[0]) ? $name[0] : "#");
		if($prefix < 'A' || $prefix > 'Z') {
			$prefix = '#';
		}
		$prefix = "{$prefix}";
		if(!isset($lists[$prefix])) {
			$lists[$prefix] = array(
				"prefix" => $prefix,
				"list" => array()
			);
		}

		$aux = array(
			"name" => $comic->name,
			"title" => $comic->name,
			"iconid" => $iconId,
			"icon" => $comic->previewUri(),
			"url" => "?action=".WC_ACTION_COMIC_SHOW."&group={$gid}&comic={$comic->id}",
		);
		if($wcProfile->allowedTo(WC_PERM_COMIC_SET_RATE, WC_PERM_COMIC_SET_READ)) {
			$aux["widget"] = "/generics/list-view-comic-widget.html";

			$readRate = $wcReadRate->comicReadRate($comic);
			$aux["w-global-rate"] = $readRate["rate"];

			$readRate = $wcReadRate->readRate($comic);
			$aux["w-rate"] = $readRate["rate"]["rate"] ? $readRate["rate"]["rate"] : 0;
			$light = $readRate["read"]["read"] ? "on" : "off";
			$aux["w-light"] = $wcDefaults["icon-light-{$light}"];
		}
		$lists[$prefix]["list"][] = $aux;

		$zoom[] = array(
			"id" => $iconId,
			"path" => $comic->previewUri()
		);

		$iconId++;
	}
	ksort($lists);

	if(count($lists) == 0) {
		$lists[] = array(
			"prefix" => "No available members",
			"list" => array()
		);
	}

	$wcThemeAssigns["MenuLists"] = $lists;
	$wcThemeAssigns["MenuListsZoom"] = $zoom;

	$wcThemeAssigns["MenuListsInfo"] = array(
		array(
			"label" => "Tag",
			"value" => $wcTag->name()
		),
		array(
			"label" => "Comics",
			"value" => $wcTag->count
		)
	);
} else {
	$wcThemeAssigns["error-string"] = "You're not allowed to open tags";
	require "{$this->_themeRoot}/controlers/contents/c.403.php";
}
?>